package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;

import utils.StrUtils;

import database.Clause;
import database.ValuedAttribute;

/**
 * Classe de communication avec la table 'member' de la BDD
 * @author Aubry Pirian
 *
 */
public class Member extends DbTable {
	
	private int id;				// numéro du membre
	private String login;		// pseudonyme du membre
	private String passwd;		// password du membre
	private String email;		// adresse e-mail du membre
	private String firstname;	// prénom du membre
	private String lastname;	// nom du membre
	private char gender;		// sexe du membre
	private String rank;		// rang du membre (user, admin, ...)
	private String status;		// statut de l'inscription (new, accepted, ...)
	private String date_reg;	// date d'inscription
	
	
	/**
	 * Constructeur sans paramètre
	 * @throws ClassNotFoundException	: Impossible de charger le driver JDBC
	 * @throws SQLException
	 * @throws NoSuchFieldException		: Attribut non valide
	 */
	public Member () throws ClassNotFoundException,SQLException,NoSuchFieldException {
		super(Params.MEMBER_TABLE);
	}
	
	/**
	 * Constructeur avec identifiant
	 * @throws ClassNotFoundException	: Impossible de charger le driver JDBC
	 * @throws SQLException
	 * @throws NoSuchFieldException		: Attribut non valide
	 */
	public Member (String login) throws ClassNotFoundException,SQLException,NoSuchFieldException {
		super(Params.MEMBER_TABLE);
		this.login=login;
		try {
			this.getInfos(login);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
	}
	
	/**
	 * Constructeur avec numéro
	 * @throws NoSuchFieldException 
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 * @throws ClassNotFoundException	: impossible de charger le driver JDBC
	 * @throws SQLException
	 * @throws NoSuchFieldException		: paramètre non valide
	 */
	public Member (int id) throws ClassNotFoundException, SQLException, NoSuchFieldException {
		super(Params.MEMBER_TABLE);
		this.id=id;
		try {
			this.getInfos(id);
		} catch (SQLException e) {
			throw e;
		}
	}

	/**
	 * Accesseur du numéro
	 * @return numéro du membre
	 */
	public int getId() { return this.id; }

	/**
	 * Accesseur du pseudonyme
	 * @return pseudonyme du membre
	 */
	public String getLogin() { return this.login; }

	/**
	 * Accesseur de l'email
	 * @return adresse email du membre
	 */
	public String getEmail() { return this.email; }

	/**
	 * Accesseur du prénom
	 * @return prénom du membre
	 */
	public String getFirstName() { return this.firstname; }

	/**
	 * Accesseur du nom
	 * @return nom du membre
	 */
	public String getLastName() { return this.lastname; }

	/**
	 * Accesseur du sexe
	 * @return sexe du membre
	 */
	public char getGender() { return this.gender; }

	/**
	 * Accesseur du rang
	 * @return rang du membre
	 */
	public String getRank() { return this.rank; }

	/**
	 * Accesseur du pseudonyme
	 * @return statut du membre
	 */
	public String getStatus() { return this.status; }

	/**
	 * Accesseur de la date d'inscription
	 * @return date d'inscription
	 */
	public String getDateRegistered() { return this.date_reg; }
	
	
	/**
	 * Modifieur de l'indentifiant
	 * @param login : nouvel identifiant
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setId(int id) throws NoSuchFieldException, SQLException {
		if (id<0) throw new NoSuchFieldException("Numéro invalide");
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		try {
			values.add( new ValuedAttribute("id",id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		try {
			this.idbc.update(this.tableName, values, where);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		this.id=id;
	}
	
	
	/**
	 * Modifieur de l'indentifiant
	 * @param login : nouvel identifiant
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setLogin(String login) throws NoSuchFieldException, SQLException {
		if (login==null) throw new NoSuchFieldException("Identifiant vide");
		if (login.length()<Params.SIZES_LOGIN[0]) throw new NoSuchFieldException("Identifiant trop court");
		if (login.length()>Params.SIZES_LOGIN[1]) throw new NoSuchFieldException("Identifiant trop long");
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		try {
			values.add( new ValuedAttribute("login",login) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		try {
			this.idbc.update(this.tableName, values, where);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		this.login = login;
	}
	
	/**
	 * Modifieur du mot de passe
	 * @param passwd : nouveau mot de passe
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setPasswd (String passwd) throws NoSuchFieldException, SQLException {
		if ( passwd==null || passwd.length()==0 ) throw new NoSuchFieldException("Mot de passe vide");
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		try {
			values.add( new ValuedAttribute("passwd",passwd) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		try {
			this.idbc.update(this.tableName, values, where);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		this.passwd = passwd;
		
	}
	
	/**
	 * 
	 * @param email : nouvelle adresse email
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setEmail(String email) throws NoSuchFieldException, SQLException {
		if (email==null) throw new NoSuchFieldException("Email vide");
		if (email.length()<Params.SIZES_EMAIL[0]) throw new NoSuchFieldException("Email trop court");
		if (email.length()>Params.SIZES_EMAIL[1]) throw new NoSuchFieldException("Email trop long");
		//TODO : vérifier le format
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		try {
			values.add( new ValuedAttribute("email",email) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		try {
			this.idbc.update(this.tableName, values, where);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		this.email = email;
	}
	
	/**
	 * Modifieur du nom complet
	 * @param firstname : nouveau prénom
	 * @param lastname : nouveau nom
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setName(String firstname, String lastname) throws NoSuchFieldException, SQLException {
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		if (firstname!=null) {
			if (firstname.length()<Params.SIZES_FIRSTNAME[0]) throw new NoSuchFieldException("Prénom trop court");
			if (firstname.length()>Params.SIZES_FIRSTNAME[1]) throw new NoSuchFieldException("Prénom trop long");
			try {
				values.add( new ValuedAttribute("firstname",firstname) );
			} catch (NoSuchFieldException e) {
				throw e;
			}
		}
		if (lastname!=null) {
			if (lastname.length()<Params.SIZES_LASTNAME[0]) throw new NoSuchFieldException("Nom trop court");
			if (lastname.length()>Params.SIZES_LASTNAME[1]) throw new NoSuchFieldException("Nom trop long");
			try {
				values.add( new ValuedAttribute("lastname",lastname) );
			} catch (NoSuchFieldException e) {
				throw e;
			}
		}
		if (values.size()>0) {
			try {
				this.idbc.update(this.tableName, values, where);
			} catch (SQLException e) {
				throw e;
			} catch (NoSuchFieldException e) {
				throw e;
			}
			if (firstname!=null) this.firstname = firstname;
			if (lastname!=null) this.lastname = lastname;
		}
	}
	
	/**
	 * Modifieur du prénom
	 * @param firstname : nouveau prénom
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setFirstname(String firstname) throws NoSuchFieldException, SQLException {
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		if (firstname!=null) {
			if (firstname.length()<Params.SIZES_FIRSTNAME[0]) throw new NoSuchFieldException("Prénom trop court");
			if (firstname.length()>Params.SIZES_FIRSTNAME[1]) throw new NoSuchFieldException("Prénom trop long");
			try {
				values.add( new ValuedAttribute("firstname",firstname) );
			} catch (NoSuchFieldException e) {
				throw e;
			}
		}
		if (values.size()>0) {
			try {
				this.idbc.update(this.tableName, values, where);
			} catch (SQLException e) {
				throw e;
			} catch (NoSuchFieldException e) {
				throw e;
			}
			if (firstname!=null) this.firstname = firstname;
		}
	}
	
	/**
	 * Modifieur du nom
	 * @param firstname : nouveau prénom
	 * @param lastname : nouveau nom
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setLastname(String lastname) throws NoSuchFieldException, SQLException {
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		if (lastname!=null) {
			if (lastname.length()<Params.SIZES_LASTNAME[0]) throw new NoSuchFieldException("Nom trop court");
			if (lastname.length()>Params.SIZES_LASTNAME[1]) throw new NoSuchFieldException("Nom trop long");
			try {
				values.add( new ValuedAttribute("lastname",lastname) );
			} catch (NoSuchFieldException e) {
				throw e;
			}
		}
		if (values.size()>0) {
			try {
				this.idbc.update(this.tableName, values, where);
			} catch (SQLException e) {
				throw e;
			} catch (NoSuchFieldException e) {
				throw e;
			}
			if (lastname!=null) this.lastname = lastname;
		}
	}
	
	/**
	 * Modifieur du sexe
	 * @param gender : nouveau sexe
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setGender(String gender) throws NoSuchFieldException, SQLException {
		if ( gender!=null && gender.length()>=1 ) this.setGender(gender.charAt(0));
		else this.setGender(Params.BLANK_GENDER);
	}
	
	/**
	 * Modifieur du sexe
	 * @param gender : nouveau sexe
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setGender(char gender) throws NoSuchFieldException, SQLException {
		System.out.println(Params.TABLE_ATTRIBUTES[0][6][2]);
		if (!StrUtils.inArray(StrUtils.parseList(Params.TABLE_ATTRIBUTES[0][6][2]),gender,false))
			throw new NoSuchFieldException("Sexe inconnu.");
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		try {
			values.add( new ValuedAttribute("gender",""+gender) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		try {
			this.idbc.update(this.tableName, values, where);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		this.gender = gender;
	}
	
	/**
	 * Modifieur du rang
	 * @param rank : nouveau rang
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setRank(String rank) throws NoSuchFieldException, SQLException {
		if (!StrUtils.inArray(StrUtils.parseList(Params.TABLE_ATTRIBUTES[0][7][2]),rank,false))
			throw new NoSuchFieldException("Rang inconnu.");
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		try {
			values.add( new ValuedAttribute("rank",rank.toLowerCase()) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		try {
			this.idbc.update(this.tableName, values, where);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		this.rank = rank;
	}
	
	/**
	 * Modifieur du statut
	 * @param status : nouveau statut
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public void setStatus(String status) throws NoSuchFieldException, SQLException {
		if (!StrUtils.inArray(StrUtils.parseList(Params.TABLE_ATTRIBUTES[0][8][2]),status,false))
			throw new NoSuchFieldException("Statut inconnu.");
		Clause where;
		try {
			where = new Clause ( new ValuedAttribute("id",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		LinkedList<ValuedAttribute> values = new LinkedList<ValuedAttribute>();
		try {
			values.add( new ValuedAttribute("status",status.toLowerCase()) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		try {
			this.idbc.update(this.tableName, values, where);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		this.status = status;
	}
	
	
	/**
	 * Authentifie le membre avec un mot de passe et un rang donnés
	 * @param passwd	: mot de passe
	 * @param rank		: rang
	 * @return			: true/false
	 */
	public boolean auth (String passwd, String rank) {
		if (passwd==null) return false;
		if (!this.status.equalsIgnoreCase("accepted")) return false;
		if (!this.passwd.equals(passwd)) return false;
		if (rank!=null) return this.rank.equalsIgnoreCase("rank");
		return true;
	}
	
	/**
	 * Authentifie le membre avec un mot de passe donné
	 * @param passwd	: mot de passe
	 * @return			: true/false
	 */
	public boolean auth (String passwd) {
		return this.auth(passwd,null);
	}

	/**
	 * Méthode récupérant des informations dans la base de données
	 * @param login	: pseudonyme du membre
	 * @param infos	: infos à récupérer
	 */
	private void getInfos (String login, LinkedList<String> infos)
			throws SQLException,NoSuchFieldException {
		ValuedAttribute va;
		if (login!=null) {
			try {
				va = new ValuedAttribute("login",login);
			} catch (NoSuchFieldException e) {
				throw e;
			}
		} else {
			throw new NoSuchFieldException("Login manquant.");
		}
		Clause where = new Clause(va);
		ResultSet rs;
		try {
			rs = this.idbc.select(this.tableName,infos,where);
			try {
				if (rs.next()) {
					try {
						this.id = rs.getInt("id");
						this.login = login;
						this.email = rs.getString("email");
						this.passwd = rs.getString("passwd");
						this.firstname = rs.getString("firstname");
						this.lastname = rs.getString("lastname");
						this.gender = rs.getString("gender").charAt(0);
						this.rank = rs.getString("rank");
						this.status = rs.getString("status");
						this.date_reg = rs.getString("date_reg");
					} catch (SQLException e) {
						throw e;
					}
				} else {
					throw new SQLException("Utilisateur inconnu.");
				}
			} catch (SQLException e) {
				throw e;
			}
		} catch (SQLException e) {
			throw e;
		}
	}

	/**
	 * Méthode récupérant les informations dans la base de données
	 * @param login	: pseudonyme du membre
	 */
	private void getInfos (String login) throws SQLException,NoSuchFieldException {
		try {
			this.getInfos(login,null);
		} catch (NoSuchFieldException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}
	}

	/**
	 * Méthode récupérant des informations dans la base de données
	 * @param id	: numéro du membre
	 * @param infos	: informations à récupérer
	 */
	private void getInfos (int id, LinkedList<String> infos) throws NoSuchFieldException,SQLException {
		ValuedAttribute va;
		try {
			va = new ValuedAttribute("id",id);
		} catch (NoSuchFieldException e) {
			throw e;
		}
		Clause where = new Clause(va);
		ResultSet rs;
		try {
			rs = this.idbc.select(this.tableName,infos,where);
		} catch (SQLException e) {
			throw e;
		}
		try {
			if (rs.next()) {
				try {
					this.login = rs.getString("login");
					this.email = rs.getString("email");
					this.passwd = rs.getString("passwd");
					this.firstname = rs.getString("firstname");
					this.lastname = rs.getString("lastname");
					String g = rs.getString("gender");
					if ( g!=null && g.length()>=1 ) this.gender = g.charAt(0);
					else this.gender=' ';
					this.rank = rs.getString("rank");
					this.status = rs.getString("status");
					this.date_reg = rs.getString("date_reg");
				} catch (SQLException e) {
					throw e;
				}
			} else {
				throw new SQLException("Utilisateur inconnu.","No result found");
			}
		} catch (SQLException e) {
			throw e;
		}
	}

	/**
	 * Méthode récupérant toutes les informations dans la base de données
	 * @param id	: numéro du membre
	 * @throws NoSuchFieldException : paramètre non valide 
	 * @throws SQLException 
	 */
	private void getInfos (int id) throws NoSuchFieldException, SQLException {
		try {
			this.getInfos(id,null);
		} catch (NoSuchFieldException e) {
			throw e;
		} catch (SQLException e) {
			throw e;
		}
	}
	
	/**
	 * Méthode retournant le nombre de connexions du membre dans une période
	 * @param room	: salle visitée
	 * @param beg	: date minimum ( 'day'->jour ; 'month'->mois; 'year'->année )
	 * @param end	: date maximum ( 'day'->jour ; 'month'->mois; 'year'->année )
	 * @return nombre de connexions
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException
	 */
	public int getConnections(int room, HashMap<String, Integer> beg, HashMap<String, Integer> end) throws NoSuchFieldException, SQLException {
		Clause where;
		try {
			where = new Clause( new ValuedAttribute("member",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		if (room!=-1) {
			Clause rc;
			try {
				rc = new Clause( new ValuedAttribute("room",room) );
			} catch (NoSuchFieldException e) {
				throw e;
			}
			try {
				where = new Clause ("AND", where, rc);
			} catch (NoSuchFieldException e) {
				throw e;
			}
		}
		if (beg!=null) {
			String d = "";
			int m;
			if (beg.get("year")!=null) {
				d += beg.get("year");
				if (beg.get("month")!=null) {
					m = beg.get("month");
					d += "-";
					if (m<10) d += "0";
					d += beg.get("month");
					if (beg.get("day")!=null) {
						m = beg.get("day");
						d += "-";
						if (m<10) d += "0";
						d += beg.get("day");
					}// else d+="-00";
				}// else d+="-00-00";
				Clause dc;
				try {
					dc = new Clause( new ValuedAttribute("date_out",d,">") );
				} catch (NoSuchFieldException e) {
					throw e;
				}
				try {
					where = new Clause ("AND", where, dc);
				} catch (NoSuchFieldException e) {
					throw e;
				}
			}
		}
		if (end!=null) {
			String d = "";
			int m;
			if (end.get("year")!=null) {
				d += end.get("year");
				if (end.get("month")!=null) {
					m = end.get("month");
					d += "-";
					if (m<10) d += "0";
					d += end.get("month");
					if (end.get("day")!=null) {
						m = end.get("day");
						d += "-";
						if (m<10) d += "0";
						d += end.get("day");
					}// else d+="-00";
				}// else d+="-00-00";
				Clause dc = new Clause( new ValuedAttribute("date_in",d,"<") );
				where = new Clause ("AND", where, dc);
			}
		}
		ResultSet rs;
		try {
			rs = this.idbc.select(Params.CONNECTION_TABLE,where);
		} catch (SQLException e) {
			throw e;
		}
		int count = 0;
		try {
			while (rs.next()) count+=1;
		} catch (SQLException e) {
			throw e;
		}
		return count;
	}
	
	/**
	 * Méthode retournant le nombre de connexions du membre, dans une période
	 * @param beg	: date minimum ( 'day'->jour ; 'month'->mois; 'year'->année )
	 * @param end	: date maximum ( 'day'->jour ; 'month'->mois; 'year'->année )
	 * @return nombre de connexions
	 * @throws SQLException 
	 * @throws NoSuchFieldException : paramètre non valide
	 */
	public int getConnections(HashMap<String, Integer> beg, HashMap<String, Integer> end) throws NoSuchFieldException, SQLException {
		return getConnections(-1, beg, end);
	}

	
	/**
	 * Méthode retournant le nombre de connexions du membre dans une salle, à une date données
	 * @param room	: salle visitée
	 * @param date	: date de connexion ( 'day'->jour ; 'month'->mois; 'year'->année )
	 * @return nombre de connexions
	 * @throws SQLException 
	 * @throws NoSuchFieldException : paramètre non valide
	 */
	public int getConnections(int room, HashMap<String, Integer> date) throws SQLException, NoSuchFieldException {
		Clause where;
		try {
			where = new Clause( new ValuedAttribute("member",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		if (room!=-1) {
			Clause rc;
			try {
				rc = new Clause( new ValuedAttribute("room",room) );
			} catch (NoSuchFieldException e) {
				throw e;
			}
			try {
				where = new Clause ("AND", where, rc);
			} catch (NoSuchFieldException e) {
				throw e;
			}
		}
		if (date!=null) {
			String d = "";
			if (date.get("year")!=null) d += date.get("year");
			else d += "%";
			d += "-";
			if (date.get("month")!=null) {
				int m = date.get("month");
				if (m<10) d += "0";
				d += date.get("month");
			} else d += "%";
			d += "-";
			if (date.get("day")!=null) {
				int m = date.get("day");
				if (m<10) d += "0";
				d += date.get("day");
			} else d += "%";
			d += " %";
			Clause dc;
			try {
				dc = new Clause( new ValuedAttribute("date_in",d,"like") );
			} catch (NoSuchFieldException e) {
				throw e;
			}
			try {
				where = new Clause ("AND", where, dc);
			} catch (NoSuchFieldException e) {
				throw e;
			}
		}
		ResultSet rs;
		try {
			rs = this.idbc.select(Params.CONNECTION_TABLE,where);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		int count = 0;
		try {
			while (rs.next()) {
				count+=1;
			}
		} catch (SQLException e) {
			throw e;
		}
		return count;
	}

	/**
	 * Méthode retournant le nombre de connexions du membre
	 * @param date	: date de connexion ( 'day'->jour ; 'month'->mois; 'year'->année )
	 * @return nombre de connexions
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public int getConnections(HashMap<String, Integer> date) throws SQLException, NoSuchFieldException {
		return getConnections(-1, date);
	}

	/**
	 * Méthode retournant le nombre de connexions du membre dans une salle
	 * @param room	: salle visitée
	 * @return nombre de connexions
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public int getConnections(int room) throws SQLException, NoSuchFieldException {
		return getConnections(room, null);
	}

	/**
	 * Méthode retournant le nombre de connexions du membre
	 * @return nombre de connexions
	 * @throws NoSuchFieldException : paramètre non valide
	 * @throws SQLException 
	 */
	public int getConnections() throws SQLException, NoSuchFieldException {
		return getConnections(-1, null);
	}

	/**
	 * Méthode retournant le pourcentage de bonnes réponses du membre
	 * @return pourcentage de bonnes réponses
	 * @throws SQLException 
	 * @throws NoSuchFieldException : paramètre non valide
	 */
	public float getGoodAnswers() throws SQLException, NoSuchFieldException {
		LinkedList<String> attrs = new LinkedList<String>();
		attrs.add("right");
		Clause where;
		try {
			where = new Clause( new ValuedAttribute("member",this.id) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		ResultSet rs;
		try {
			rs = this.idbc.select(model.trivial_pursuit.TPParams.ANSWERED_TABLE, attrs, where);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		int count=0;
		int countGood=0;
		try {
			while (rs.next()) {
				try {
					if (rs.getInt("right")==1) countGood++;
				} catch (SQLException e) {
					throw e;
				}
				count++;
			}
		} catch (SQLException e) {
			throw e;
		}
		if (count!=0) return countGood*100/count;
		else return 0;
	}
	
	
	/**
	 * Méthode créant un membre dans la base de données
	 * @param login
	 * @param email
	 * @param passwd
	 * @return un numéro d'erreur:
	 * 		0 : pas d'erreur
	 * 		1 : login existant
	 * @throws SQLException
	 * @throws NoSuchFieldException : paramètre non valide
	 */
	public void create(String login, String email, String passwd) throws SQLException, NoSuchFieldException {
		LinkedList<ValuedAttribute> attrs = new LinkedList<ValuedAttribute>();
		try {
			attrs.add( new ValuedAttribute("login",login) );
			attrs.add( new ValuedAttribute("email",email) );
			attrs.add( new ValuedAttribute("passwd",passwd) );
		} catch (NoSuchFieldException e) {
			throw e;
		}
		try {
			this.idbc.insert(this.tableName, attrs);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
		this.login = login;
		this.email = email;
		this.passwd = passwd;
		LinkedList<String> infos = new LinkedList<String>();
		infos.add("id");
		try {
			this.getInfos(login,infos);
		} catch (SQLException e) {
			throw e;
		} catch (NoSuchFieldException e) {
			throw e;
		}
	}

	
	@Override
	public String toString() {
		String s = "Member (";
		s += this.id+"/";
		s += this.login+"/";
		s += this.email+"/";
		s += this.passwd+"/";
		s += this.firstname+"/";
		s += this.lastname+"/";
		s += this.rank+"/";
		s += this.status;
		s += ")";
		return s;
	}
	
	
	
	public static void main (String[] args) {
		//FIXME : supprimer ce main
			Member m;
			try {
				m = new Member("aubry");
				System.out.println(m);
				Stats.main(null);
				m = new Member();
				m.create("john", "john@qsdnfn.fr", "passwd");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
	}

}
